package com.sinodata.bsm.cicp.collector.sybase;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.AbstractJDBCCollector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * Database.java
 * <p>
 *
 * @author 管普贵 guanpg@uni-mon.com
 * @version 2.0 
 * @Date Jan 8, 2010
 */
public class Database extends AbstractJDBCCollector {

    private static final Logger logger = Logger.getLogger(Database.class);

    private static String getStatusInfo(int state) {
        String[] status = new String[] { "Upgrade started on this database", "Upgrade has been successful", "select into/bulkcopy", "trunc log on chkpt", "no chkpt on recovery", "Database created with for load option, or crashed while loading database, instructs recovery not to proceed", "Recovery started for all databases to be recovered", "", "Database suspect,Not recovered,Cannot be opened or used,Can be dropped only with dbcc dbrepair", "ddl in tran", "read only", "dbo use only", "single user", "allow nulls by default" };
        StringBuffer sb = new StringBuffer("");
        if (state > 0) {
            for (int i = 0; i < status.length; i++) {
                if (((state >> i) & 0x01) == 0x01) {
                    sb.append(status[i] + " ");
                }
            }
        }
        return sb.toString().equals("") ? "N/A" : sb.toString();

    }

    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        try {
            String[] propMetas = getPropMeta();
            //        String serverName = this.getParamMap().get("服务器名");
            //        String dataName = this.getParamMap().get("数据库名");
            String serverName = this.getParamMap().get("ServerName");
            String dataName = this.getParamMap().get("DBName");
            String sql = "select status=d.status from master..sysdatabases d where d.name='" + dataName + "'";
            String[] rs = this.getStrings(serverName, sql, ip);

            ResPropertyValue propertyValueX = new ResPropertyValue();
            ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
            for (int i = 0; i < propMetas.length; i++) {
                propValues[i] = ResPropertyValue.createPropValue(rs[i], propMetas[i]);
            }

            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
        } catch (Exception ex) {
            logger.error("failed to collect sybase database", ex);
            throw new CollectorException(ex);
        }
        return values;
    }

    private String[] getStrings(String serverName, String sql, String ip) throws CollectorException {
        String[] propValArr = new String[1];

        try {
            ResultSet rst = this.getCachedResultSet("sybase", ip, serverName, sql);
            int status = 0;
            while (rst.next()) {
                status = rst.getInt("status");
            }
            propValArr[0] = getStatusInfo(status);
        } catch (Exception ex) {
            logger.error("while connect database : ", ex);
            throw new CollectorException(ex);
        }

        return propValArr;
    }

    public String[] getParamMeta() {
        //        return new String[] { "服务器名", "数据库名" };
        return new String[] { "ServerName", "DBName" };
    }

    public String[] getPropMeta() {
        //        return new String[] { "状态" };
        return new String[] { "Status" };
    }
}
